package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBeanCondition;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.ExportExcelUtil;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
	@Autowired
	JedisPool jedisPool;
	@Reference
	private SetmealService setmealService;
	//文件上传
	@RequestMapping("/upload")
	public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
		try {
			String originalFilename = imgFile.getOriginalFilename();
			int index = originalFilename.lastIndexOf(".");
			String suffix = originalFilename.substring(index - 1);
			String fileName = UUID.randomUUID().toString()+suffix;
			//调用七牛云将文件上传到服务器
			QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
			Result result = new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
			result.setData(fileName);
			//将上传图片名称存入Redis，基于Redis的Set集合存储
			jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
			return result;
		} catch (IOException e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
		}
	}
	@RequestMapping("/add")
	public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
		try{
			setmealService.add(setmeal,checkgroupIds);
		}catch (Exception e){
			e.printStackTrace();
			//服务调用失败
			return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
		}
		return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
	}
	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBeanCondition queryPageBeanCondition){
		return setmealService.findPage(queryPageBeanCondition);
	}
	@RequestMapping("/findSetmeal")
	public Result findSetmeal(@RequestBody Map<String,String> map){
		try {
			String queryGroupName = map.get("queryGroupName");
			List<Setmeal> setmeal = setmealService.findSetmeal(queryGroupName);
			return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
		}
	}
//	@RequestMapping("/exportExcel")
//	public void exportExcel(@RequestBody List<Map<String,String>> dataList)throws IOException{
//		ExportExcelUtil.exportExcel2FilePath("sheet1","E:\\",null,dataList);
//	}
}
